/*********************************************************/
/*                                                       */
/* Warleague                                             */
/*                                                       */
/* menustate.cpp                                         */
/*                                                       */
/*********************************************************/

/*********************************************************/
/* includes                                              */
/*********************************************************/
#include <SFML/Graphics.hpp>
#include "gamestate.h"
#include "engine.h"
#include "menustate.h"
#include "playstate.h"
#include <iostream>

CMenuState CMenuState::m_MenuState;

void CMenuState::Init()
{
    // load buttons for the menu
    button_new.Init(0, 100, "images/button_new.png", "images/button_new_over.png");
    button_new.CenterWidth(800);
    button_load.Init(0, 200, "images/button_load.png", "images/button_load_over.png");
    button_load.CenterWidth(800);
    button_options.Init(0, 300, "images/button_options.png", "images/button_options_over.png");
    button_options.CenterWidth(800);
    button_quit.Init(0, 400, "images/button_quit.png", "images/button_quit_over.png");
    button_quit.CenterWidth(800);
    button_new.SetAlpha(255);
    button_load.SetAlpha(255);
    button_options.SetAlpha(255);
    button_quit.SetAlpha(255);
}

void CMenuState::Cleanup()
{

}

void CMenuState::Pause()
{
}

void CMenuState::Resume()
{
}

void CMenuState::HandleEvents(CGameEngine* game)
{
    // Process events
    sf::Event Event;
    while (game->App->PollEvent(Event))
    {
        button_new.Checkcollision(Event.MouseMove.X, Event.MouseMove.Y);
        button_load.Checkcollision(Event.MouseMove.X, Event.MouseMove.Y);
        button_options.Checkcollision(Event.MouseMove.X, Event.MouseMove.Y);
        button_quit.Checkcollision(Event.MouseMove.X, Event.MouseMove.Y);

        // Close window : exit
        if (Event.Type == sf::Event::Closed)
            game->Quit();

        if (Event.Type == sf::Event::MouseButtonPressed)
        {
            if (Event.MouseButton.Button == sf::Mouse::Left)
            {
                if (button_quit.Checkcollision(Event.MouseButton.X, Event.MouseButton.Y))
                {
                    FadeOut(game);
                    game->Quit();
                }

                if (button_new.Checkcollision(Event.MouseButton.X, Event.MouseButton.Y))
                {
                    FadeOut(game);
                    game->ChangeState(CPlayState::Instance());
                }
            }
        }



        // A key has been pressed
        if (Event.Type == sf::Event::KeyPressed)
        {
            FadeOut(game);
            game->Quit();
        }
    }
}

void CMenuState::Update(CGameEngine* game)
{
}

void CMenuState::Draw(CGameEngine* game)
{
    game->App->Clear (sf::Color (0, 0, 0));

    game->App->Draw(button_new.GetSprite());
    game->App->Draw(button_load.GetSprite());
    game->App->Draw(button_options.GetSprite());
    game->App->Draw(button_quit.GetSprite());

    game->App->Display ();
}

void CMenuState::FadeOut(CGameEngine* game)
{
    for (int i = 0; i < 255; i++)
    {
        button_new.SetAlpha(255-i);
        button_load.SetAlpha(255-i);
        button_options.SetAlpha(255-i);
        button_quit.SetAlpha(255-i);

        Draw (game);
    }
}
